<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:inv="http://www.quattroclix.com/invoice"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:date="http://exslt.org/dates-and-times"
    xsi:schemaLocation="http://www.quattroclix.com/invoice file:///home/pierre/Dev/invoice/trunk/Invoicing/InvoicingCore/src/main/resources/invoice.xsd http://www.w3.org/1999/XSL/Format http://www.xmlblueprint.com/documents/fop.xsd"
    exclude-result-prefixes="xs inv"
    extension-element-prefixes="date"
    version="1.0">

    <xsl:output indent="yes"/>
    
    <xsl:template match="/inv:timesheet">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master margin-bottom="7mm" margin-left="10mm" margin-right="10mm"
                    margin-top="5mm" master-name="invoice-page-master">
                    <fo:region-body margin-bottom="5mm" margin-top="10mm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="invoice-page-master" id="main-sequence">

                <fo:flow flow-name="xsl-region-body">
                    <xsl:call-template name="header">
                        <xsl:with-param name="name">
                            <xsl:value-of select="/inv:timesheet/inv:consultant/text()"/>
                        </xsl:with-param>
                        <xsl:with-param name="client">
                            <xsl:value-of select="/inv:timesheet/inv:client-name/text()"/>
                        </xsl:with-param>
                        <xsl:with-param name="period-marker">
                            <xsl:value-of select="//inv:day[7]/inv:date/text()"/>
                        </xsl:with-param>
                        <xsl:with-param name="project">
                            <xsl:value-of select="/inv:timesheet/inv:project-name/text()"/>
                        </xsl:with-param>
                    </xsl:call-template>

                    <xsl:apply-templates select="//inv:day[date:day-in-week(inv:date/text()) = 1]"/>

                    <xsl:call-template name="signature-boxes">
                        <xsl:with-param name="responsible-person">
                            <xsl:value-of select="//inv:responsible-person"/>
                        </xsl:with-param>
                        <xsl:with-param name="consultant">
                            <xsl:value-of select="/inv:timesheet/inv:consultant/text()"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </fo:flow>

            </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template name="header">
        <xsl:param name="name"/>
        <xsl:param name="period-marker"/>
        <xsl:param name="client"/>
        <xsl:param name="project"/>

        <fo:block text-align="center" space-after="5mm">
            <fo:table table-layout="fixed">
                <fo:table-column column-width="50%"/>
                <fo:table-column column-width="50%"/>
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell text-align="right" padding-bottom="5mm">
                            <fo:block font-size="24pt">Timesheet :</fo:block>
                        </fo:table-cell>
                        <fo:table-cell text-align="left" padding-left="5mm"  padding-bottom="5mm">
                            <fo:block font-size="24pt">
                                <xsl:value-of select="$name"/>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell text-align="right">
                            <fo:block font-size="18pt">Period :</fo:block>
                        </fo:table-cell>
                        <fo:table-cell text-align="left" padding-left="5mm">
                            <fo:block font-size="18pt">
                                <xsl:call-template name="period">
                                    <xsl:with-param name="date" select="$period-marker"/>
                                </xsl:call-template>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell text-align="right">
                            <fo:block font-size="18pt">Client :</fo:block>
                        </fo:table-cell>
                        <fo:table-cell text-align="left" padding-left="5mm">
                            <fo:block font-size="18pt">
                                <xsl:value-of select="$client"/>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row>
                        <fo:table-cell text-align="right">
                            <fo:block font-size="18pt">Project :</fo:block>
                        </fo:table-cell>
                        <fo:table-cell text-align="left" padding-left="5mm">
                            <fo:block font-size="18pt">
                                <xsl:value-of select="$project"/>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:block>
    </xsl:template>

    <xsl:template name="period">
        <xsl:param name="date"/>
        <xsl:value-of select="date:month-name($date)"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="date:year($date)"/>
    </xsl:template>

    <xsl:template name="display-day">
        <xsl:param name="day"/>

        <fo:table-cell display-align="center" height="21mm">
            <xsl:attribute name="background-color">
                <xsl:if test="$day/inv:weekend = 'true'">
                    black
                </xsl:if>
                <xsl:if test="$day/inv:weekend = 'false' and $day/inv:holiday = 'true'">
                    gray
                </xsl:if>
                <xsl:if test="$day/inv:weekend = 'false' and $day/inv:part-of-invoice = 'false'">
                    gray
                </xsl:if>
                <xsl:if test="$day/inv:weekend = 'false' and $day/inv:part-of-invoice = 'true'">
                    white
                </xsl:if>
            </xsl:attribute>
            <fo:table border-left-style="solid" border-right-style="solid" table-layout="fixed">
                <fo:table-column column-width="100%"/>
                <fo:table-body>
                    <fo:table-row height="7mm">
                        <fo:table-cell display-align="center" border-bottom-style="solid">
                            <fo:block font-size="12pt" text-align="center">
                                <xsl:attribute name="color">
                                    <xsl:if test="$day/inv:weekend = 'true'">
                                        white
                                    </xsl:if>
                                    <xsl:if test="$day/inv:weekend = 'false'">
                                        black
                                    </xsl:if>
                                </xsl:attribute>
                                <xsl:call-template name="formatdate">
                                    <xsl:with-param name="date">
                                        <xsl:value-of select="$day/inv:date/text()"/>
                                    </xsl:with-param>
                                </xsl:call-template>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row height="7mm">
                        <fo:table-cell display-align="center" border-bottom-style="solid">
                            <fo:block font-size="12pt" text-align="center">
                                <xsl:attribute name="color">
                                    <xsl:if test="$day/inv:weekend = 'true'">
                                        white
                                    </xsl:if>
                                    <xsl:if test="$day/inv:weekend = 'false'">
                                        black
                                    </xsl:if>
                                </xsl:attribute>
                                <xsl:if test="$day/inv:part-of-invoice = 'true'">
                                    <xsl:value-of select="$day/inv:worked-std-hours/text()"/>
                                </xsl:if>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row height="7mm">
                        <fo:table-cell display-align="center">
                            <fo:block font-size="12pt" text-align="center">
                                <xsl:attribute name="color">
                                    <xsl:if test="$day/inv:weekend = 'true'">
                                        white
                                    </xsl:if>
                                    <xsl:if test="$day/inv:weekend = 'false'">
                                        black
                                    </xsl:if>
                                </xsl:attribute>
                                <xsl:if test="$day/inv:part-of-invoice = 'true'">
                                    <xsl:value-of select="$day/inv:worked-over-time-hours/text()"/>
                                </xsl:if>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:table-cell>
    </xsl:template>

    <xsl:template match="//inv:day[date:day-in-week(inv:date/text()) &gt; 1 and date:day-in-week(inv:date/text()) &lt; 7]" name="working-day-template">
        <xsl:call-template name="display-day">
            <xsl:with-param name="day" select="current()"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="//inv:day[date:day-in-week(inv:date/text()) = 7]" name="saturday-template">
        <xsl:variable name="weeknumber" select="date:week-in-year(inv:date/text())"/>

        <xsl:call-template name="display-day">
            <xsl:with-param name="day" select="current()"/>
        </xsl:call-template>

        <fo:table-cell display-align="after" height="21mm">
            <fo:table table-layout="fixed">
                <fo:table-column column-width="100%"/>
                <fo:table-body>
                    <fo:table-row height="7mm">
                        <fo:table-cell display-align="center">
                            <fo:block font-size="12pt" text-align="center" border-bottom-style="solid">
                                Total
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row height="7mm">
                        <fo:table-cell display-align="center">
                            <fo:block font-size="12pt" text-align="center" border-bottom-style="solid">
                                <xsl:value-of select="sum(//inv:day[date:week-in-year(inv:date/text()) = $weeknumber and inv:part-of-invoice = 'true']/inv:worked-std-hours)"/>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                    <fo:table-row height="7mm">
                        <fo:table-cell display-align="center">
                            <fo:block font-size="12pt" text-align="center">
                                <xsl:value-of select="sum(//inv:day[date:week-in-year(inv:date/text()) = $weeknumber and inv:part-of-invoice = 'true']/inv:worked-over-time-hours)"/>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:table-cell>
    </xsl:template>

    <xsl:template match="//inv:day[date:day-in-week(inv:date/text()) = 1]" name="sunday-template">
        <xsl:variable name="weeknumber" select="date:week-in-year(inv:date/text())"/>

        <fo:block space-after="5mm">
            <fo:table border-style="solid" table-layout="fixed">
                <fo:table-column column-width="25mm"/>
                <fo:table-column column-width="40mm"/>
                <fo:table-column column-width="15mm"/>
                <fo:table-column column-width="15mm"/>
                <fo:table-column column-width="15mm"/>
                <fo:table-column column-width="15mm"/>
                <fo:table-column column-width="15mm"/>
                <fo:table-column column-width="15mm"/>
                <fo:table-column column-width="15mm"/>
                <fo:table-column column-width="20mm"/>

                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell display-align="center" padding-left="2mm">
                            <fo:table>
                                <fo:table-column column-width="100%"/>
                                <fo:table-body>
                                    <fo:table-row height="7mm">
                                        <fo:table-cell>
                                            <fo:block font-size="12pt" text-align="left" padding-left="3mm">
                                                Week <xsl:value-of select="date:week-in-year(current()/following-sibling::inv:day[position() = 3]/inv:date/text())"/>
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row height="7mm"><fo:table-cell><fo:block/></fo:table-cell></fo:table-row>
                                    <fo:table-row height="7mm"><fo:table-cell><fo:block/></fo:table-cell></fo:table-row>
                                </fo:table-body>
                            </fo:table>
                        </fo:table-cell>
                        <fo:table-cell display-align="center" height="21mm" padding-right="5mm">
                            <fo:table>
                                <fo:table-column column-width="100%"/>
                                <fo:table-body>
                                    <fo:table-row height="7mm">
                                        <fo:table-cell>
                                            <fo:block font-size="10pt" text-align="right">
                                                Date
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row height="7mm">
                                        <fo:table-cell>
                                            <fo:block font-size="10pt" text-align="right">
                                                Std Schedule Hours
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row height="7mm">
                                        <fo:table-cell>
                                            <fo:block font-size="10pt" text-align="right">
                                                Overtime Hours
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                </fo:table-body>
                            </fo:table>
                        </fo:table-cell>
                        <xsl:call-template name="display-day">
                            <xsl:with-param name="day" select="current()"/>
                        </xsl:call-template>
                        <xsl:apply-templates select="current()/following-sibling::inv:day[position() &lt; 7]"/>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:block>
    </xsl:template>

    <xsl:template name="signature-boxes">
        <xsl:param name="responsible-person"/>
        <xsl:param name="consultant"/>
        <fo:block-container absolute-position="fixed" top="230mm" left="10mm">
           <fo:block text-align="center"> <!-- the text-align doesn't seem to work -->
               <fo:table width="180mm" table-layout="fixed">
                   <fo:table-body>
                       <fo:table-row>
                           <fo:table-cell border-style="solid" height="7mm" display-align="center" text-align="center" width="85mm">
                               <fo:block><xsl:value-of select="$consultant"/></fo:block>
                           </fo:table-cell>
                           <fo:table-cell width="20mm">
                               <fo:block></fo:block>
                           </fo:table-cell>
                           <fo:table-cell border-style="solid" height="7mm" display-align="center" text-align="center" width="85mm">
                               <fo:block><xsl:value-of select="$responsible-person"/></fo:block>
                           </fo:table-cell>
                       </fo:table-row>
                       <fo:table-row>
                           <fo:table-cell border-style="solid" height="30mm" width="85mm">
                               <fo:block></fo:block>
                           </fo:table-cell>
                           <fo:table-cell width="20mm">
                               <fo:block></fo:block>
                           </fo:table-cell>
                           <fo:table-cell border-style="solid" height="30mm" width="85mm">
                               <fo:block></fo:block>
                           </fo:table-cell>
                       </fo:table-row>
                   </fo:table-body>
               </fo:table>
           </fo:block>
        </fo:block-container>
    </xsl:template>

    <xsl:template name="formatdate">
        <xsl:param name="date"/>
        <xsl:variable name="month">
            <xsl:value-of select="substring($date,6,2)"/>
        </xsl:variable>
        <xsl:variable name="day">
            <xsl:value-of select="substring($date,9,2)"/>
        </xsl:variable>
        <xsl:value-of select="concat($day,'/',$month)"/>
    </xsl:template>
    
</xsl:stylesheet>
